package com.mm.serivce.user.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.domain.user.DRole;
import com.mm.domain.user.DRolePermissionRelation;
import com.mm.domain.user.DUserRoleRelation;
import com.mm.mapper.user.DRoleMapper;
import com.mm.mapper.user.DRolePermissionRelationMapper;
import com.mm.mapper.user.DUserRoleRelationMapper;
import com.mm.serivce.user.IDRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Service
public class DRoleServiceImpl extends ServiceImpl<DRoleMapper, DRole> implements IDRoleService {

    private final DRoleMapper roleMapper;
    private final DUserRoleRelationMapper userRoleRelationMapper;
    private final DRolePermissionRelationMapper rolePermissionRelationMapper;

    public DRoleServiceImpl(DRoleMapper roleMapper, DUserRoleRelationMapper userRoleRelationMapper, DRolePermissionRelationMapper rolePermissionRelationMapper) {
        this.roleMapper = roleMapper;
        this.userRoleRelationMapper = userRoleRelationMapper;
        this.rolePermissionRelationMapper = rolePermissionRelationMapper;
    }

    @Override
    public Page<DRole> queryRolePage(Integer page, Integer limit, String roleName) {
        Page<DRole> roleIPage = new Page<>(page, limit);
        LambdaQueryWrapper<DRole> queryWrapper = new LambdaQueryWrapper<DRole>();
        if (StrUtil.isNotBlank(roleName)) {
            queryWrapper.like(DRole::getRoleName, roleName);
        }
        queryWrapper.eq(DRole::getDelFlg, '0');
        roleIPage = roleMapper.selectPage(roleIPage, queryWrapper);
        roleIPage.setTotal(roleIPage.getRecords().size());
        return roleIPage;
    }

    @Override
    public List<DRole> queryRoleList(Integer userId) {
        List<DRole> roleList = new ArrayList<>();
        LambdaQueryWrapper<DUserRoleRelation> urQueryWrapper = new LambdaQueryWrapper<DUserRoleRelation>();
        // 查询用户角色关系
        urQueryWrapper.eq(DUserRoleRelation::getUserId, userId);
        List<DUserRoleRelation> urList = userRoleRelationMapper.selectList(urQueryWrapper);
        List<Integer> roleIds = new ArrayList<>();
        if (urList.size() > 0){
            roleIds = urList.stream().map(rp -> rp.getRoleId()).collect(Collectors.toList());
            LambdaQueryWrapper<DRole> queryWrapper = new LambdaQueryWrapper<DRole>();
            queryWrapper.in(DRole::getRoleId, roleIds);
            queryWrapper.eq(DRole::getDelFlg, '0');
            roleList = roleMapper.selectList(queryWrapper);
        }
        return roleList;
    }

    @Override
    public List<DRole> queryRoleList() {
        LambdaQueryWrapper<DRole> queryWrapper = new LambdaQueryWrapper<DRole>();
        queryWrapper.eq(DRole::getDelFlg, '0');
        List<DRole> roleList = roleMapper.selectList(queryWrapper);
        return roleList;
    }

    @Override
    public DRole queryRoleOne(Integer roleId) {
        DRole roleInfo = new DRole();
        if (roleId > 0){
            roleInfo = roleMapper.selectById(roleId);
        }
        return roleInfo;
    }

    @Override
    public boolean removeRole(Integer roleId) {
        boolean flag = false;
        DRole roleInfo = roleMapper.selectById(roleId);
        if (Objects.nonNull(roleInfo)) {
            roleInfo.setDelFlg(1);
            Integer result = roleMapper.updateById(roleInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public boolean bindAuthMenu(Integer roleId, List<Integer> pIds) {
        Integer result = 0;
        // 删除角色权限关系
        LambdaQueryWrapper<DRolePermissionRelation> queryRPWrapper = new LambdaQueryWrapper();
        queryRPWrapper.eq(DRolePermissionRelation::getRoleId, roleId);
        result = rolePermissionRelationMapper.delete(queryRPWrapper);
        // 新增关系
        List<DRolePermissionRelation> rolePermissionRelations = new ArrayList<>();
        for (int i = 0; i < pIds.size(); i++) {
            rolePermissionRelations.add(DRolePermissionRelation.builder()
                    .roleId(roleId)
                    .pId(pIds.get(i))
                    .build());
        }
        result = rolePermissionRelationMapper.insertBatchSomeColumn(rolePermissionRelations);
        if (result > 0){
            return true;
        }else{
            return false;
        }
    }
}
